/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ifpe.controller;

import ifpe.transaction.TransactionManagerProxy;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author Sostenes
 */
public class ControllerFactory {

    Properties ctlProperties = new Properties();
    private static ControllerFactory instance;
    private UsuarioController usuarioController;
    private GrupoController grupoController;

    public static synchronized ControllerFactory getInstance() {
        if (instance == null) {
            instance = new ControllerFactory();
        }
        return instance;
    }

    private ControllerFactory() {
        try {
            ctlProperties.load(
                    ControllerFactory.class.getClassLoader().getResourceAsStream("ifpe/config/controller.properties"));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    public UsuarioController getUsuarioController() {
        if (usuarioController == null) {
            usuarioController = (UsuarioController) TransactionManagerProxy.newInstance(
                    (UsuarioController) getControllerInstance("usuarioController"));
        }
        return usuarioController;
    }

    public GrupoController getGrupoController() {
        if (grupoController == null) {
            grupoController = (GrupoController) TransactionManagerProxy.newInstance(
                    (GrupoController) getControllerInstance("grupoController"));
        }
        return grupoController;
    }

    private Object getControllerInstance(String property) {
        try {
            return Class.forName(ctlProperties.getProperty(property)).newInstance();
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (InstantiationException ex) {
            throw new RuntimeException(ex);
        }
    }
}
